<?php
include_once("../../php/include_all.php");
$act='';
if(isset($_POST['act'])) $act = $_POST['act'];

if($act=='simpan_usSKPD'){
	$idusSKPD = $_POST['id_usSKPD'];
	$idbidang = $_POST['id_bidang'];
	$idskpd = $_POST['skpd'];
	$idprogram = $_POST['idprogram'];
	$idkegiatan = $_POST['idkegiatan'];
	$indhslpro = $_POST['ind_hslpro'];						//2		indktor_program	
	$indklr = $_POST['ind_klr'];  							// 3	indikator_keluaran
	$indhslkeg = $_POST['ind_hslkeg'];						//1		indikator_hasil
	$targethsl = $_POST['target_hsl'];														
	$tahun = $_POST['tahun'];
	$id_rinc_usulan_skpd = $_POST['id'];
	$usulan = $_POST['usulan'];
	$lokasi = $_POST['lokasi'];
	$volume = $_POST['volume'];
	$apbdkab = $_POST['apbdkab'];
	$apbdprov = $_POST['apbdprov'];
	$apbn = $_POST['apbn'];
	$rows  = $_POST['rows'];
	
	if (!is_numeric ($idusSKPD)){
		echo "Err: invalid id";
		exit;
	}
		
	if($idusSKPD > 0) {
		exec_query("update mus_usulan_skpd set id_skpd=".$idskpd.", id_kegiatan=".$idkegiatan.", indikator_program='".$indhslpro."', indikator_keluaran='".$indklr."', indikator_hasil='".$indhslkeg."', target_hasil='".$targethsl."' where id_uskpd=".$idusSKPD);
		for ($i=0; $i<count($rows); $i++){
			exec_query("update mus_rinc_usulan_skpd set usulan='".$usulan[$i]."', lokasi='".$lokasi[$i]."', valume='".$volume[$i]."', apbdprov=".$apbdprov[$i].", apbdkab=".$apbdkab[$i].", apbn=".$apbn[$i].", prioritas=".$prioritas[$i]." where id_rinc_usulan_skpd=".$id[$i]);
		}
		echo 'success__';
		logs('Udate Usulan SKPD ','ID skpd '.$idskpd.'; ID kegiatan '.$idkegiatan.'; ');
	}else{
		$maxid = get_maxid('id_uskpd','mus_usulan_skpd');
		exec_query("insert into mus_usulan_skpd (id_uskpd, id_skpd, id_kegiatan, indikator_hasil, indikator_program, indikator_keluaran, tahun, target_hasil) values (".$maxid.",".$skpd.",".$idkegiatan.",'".$indhslkeg."','".$indhslpro."','".$indklr."',".$tahun.",'".$targethsl."')");
		$stored = get_data('select id_uskpd from mus_usulan_skpd where id_uskpd='.$maxid);
		
		//rinciaid_rinc_us_skpdn
		for ($i=0; $i<count($rows); $i++){
			$maxid1 = get_maxid('id_rinc_usulan_skpd', 'mus_rinc_usulan_skpd');
			exec_query("insert into mus_rinc_usulan_skpd(id_rinc_usulan_skpd, id_uskpd, usulan, lokasi, valume, apbdkab, apbdprov, apbn, prioritas,status) values (".$maxid1.",".$maxid.",'".$usulan[$i]."','".$lokasi[$i]."','".$volume[$i]."',".$apbdkab[$i].",".$apbdprov[$i].",".$apbn[$i].",".$prioritas[$i].",0)");
			$stored1= get_data('select id_rinc_usulan_skpd from mus_rinc_usulan_skpd where id_rinc_usulan_skpd='.$maxid1);
			if($stored1['id_rinc_usulan_skpd']==$maxid1) {
			//	echo 'success__';
				$id_rinc_usulan_skpd = $maxid1;
				}
		}
		
		if($stored['id_uskpd']==$maxid){
			echo 'success__';
			$idusSKPD = $maxid;
		}
		
		logs('Menambah usulan baru di SKPD ','ID skpd '.$skpd.'; ID kegiatan '.$idkegiatan.'; ');
	}
	
	echo $idusSKPD.'__';
	view_SKPD();
	
} else if ($act=='hapus_uSKPD'){
	$idusSKPD = $_POST['id_usSKPD'];
//	 echo $idusSKPD;
	 if (is_numeric($idusSKPD)){
		$data = get_data("select * from mus_usulan_skpd where id_uskpd=".$idusSKPD);
		exec_query("delete from mus_usulan_skpd where id_uskpd=".$idusSKPD);
		exec_query("delete from mus_rinc_usulan_skpd where id_uskpd=".$idusSKPD);
		echo 'success__';
		view_SKPD();
		
		logs('Menghapus usulan di SKPD ',implode('; ',$data));
	 }else {
		echo 'Error';
	 }
}  else if ($act=='edit_uSKPD'){
	$idusSKPD = $_POST['id_usSKPD'];
	if(is_numeric ($idusSKPD)){
		$data = get_data("select u.id_uskpd, s.id_skpd, s.nama_skpd, p.id_program,u.id_kegiatan, b.id_bdgbapeda, u.indikator_hasil, u.indikator_program, u.indikator_keluaran, u.target_hasil, u.tahun from mus_subbidangbappeda su, mus_bidang_bappeda b, mus_skpd s, mus_usulan_skpd u, mus_program p, mus_kegiatan k where u.id_skpd=s.id_skpd and s.id_subbidangbappeda=su.id_subbidangbappeda and su.id_bdgbapeda=b.id_bdgbapeda and k.id_keg=u.id_kegiatan and p.id_program=k.id_program and u.id_uskpd=".$idusSKPD);
		$dataskpd = get_datas("select * from mus_skpd where id_skpd=".$data['id_skpd']);
		$idskpd ='__';
		foreach ($dataskpd as $dataskpd){
			$idskpd .='<option value="'.$dataskpd['id_skpd'].'">'.$dataskpd['nama_skpd'].'</option>';
		}
		$ress = implode($data,'__');
		print_r ($ress);
		echo $idskpd;
		$rincskpd = get_datas("select * from mus_rinc_usulan_skpd where id_uskpd=".$data['id_uskpd']);
		echo "__<table align='center'>";
		$counter = 0;
		foreach ($rincskpd as $rincskpd){
			echo "<tr>
					<td><input type='hidden' name='id[]' value=".$rincskpd['id_rinc_usulan_skpd'].">
						<input type='text' style='width:150px;' name='usulan[]' value=".$rincskpd['usulan'].">
					</td>
					<td><input type='text' style='width:120px;' name='lokasi[]' value=".$rincskpd['lokasi']."></td>
					<td><input type='text' name='volume[]' style='width:80px;'value=".$rincskpd['valume']."></td>
					<td><input type='text' name='apbdprov[]' style='width:100px;'value=".$rincskpd['apbdprov']."></td>
					<td><input type='text' name='apbdkab[]' style='width:100px;' value=".$rincskpd['apbdkab']."></td>
					<td><input type='text' name='apbn[]' style='width:100px;' value=".$rincskpd['apbn']."></td>
					<td><input type='text' name='prioritas[]' value=".$rincskpd['prioritas']."></td>
					<td><input type='hidden' name='rows[]' value=".$counter."></td>
				</tr>";
		}
		echo "</table>";
		$prokegi = get_data("select p.nama_program, k.nama_kegiatan from mus_program p, mus_kegiatan k where p.id_program=k.id_program and k.id_keg=".$data['id_kegiatan']);
		echo "__".$prokegi['nama_program']."|".$prokegi['nama_kegiatan'];
	}else{
		echo 'error';
	}
} 

function view_SKPD() {
//	$no = 1;
   $query = get_datas("select distinct S.id_skpd, P.kode_program, P.id_program, nama_program, nama_skpd
from mus_usulan_skpd U, mus_program P, mus_kegiatan K, mus_skpd S 
where S.id_skpd=U.id_skpd and U.id_kegiatan=K.id_keg and K.id_program=P.id_program 
order by P.id_program  
");
 //tampil data
    foreach ($query as $query) {
    ?>
    <tr><td style="text-align:center;"><?php echo $query['kode_program'] ?></td>
                        <td><b><?php echo $query['nama_program']  ?></b></td>
						<td><b><?php echo $query['nama_skpd']  ?></b></td>
                        <td><b><?php echo $query['tot_apbdkab']  ?></b></td>
						<td><b><?php echo $query['tot_apbdprov']  ?></b></td>
						<td><b><?php echo $query['tot_apbn']  ?></b></td>
						<td><b><?php echo $query['tot']?></b></td>
					  	       </tr>
    <?php
    //  $no++;
	$keg= get_datas("select distinct id_kegiatan, R.id_uskpd,K.kode_kegiatan, nama_skpd,nama_kegiatan,
(select sum(ur.apbdprov) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)as tot_apbdprov,
(select sum(ur.apbdkab) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)as tot_apbdkab,
(select sum(ur.apbn) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)as tot_apbn,
((select sum(ur.apbdprov) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)+(select sum(ur.apbdkab) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)+(select sum(ur.apbn) from mus_rinc_usulan_skpd ur where R.id_uskpd=ur.id_uskpd)) as tot   
from mus_rinc_usulan_skpd R,mus_usulan_skpd U, mus_program P, mus_kegiatan K, mus_skpd S where S.id_skpd=U.id_skpd and U.id_kegiatan=K.id_keg and K.id_program=P.id_program and U.id_uskpd=R.id_uskpd and K.id_program=".$query['id_program']." and U.id_skpd=".$query['id_skpd']." ");
					foreach ($keg as $keg){
						?>
						<tr><td><?php echo $keg['kode_kegiatan']?></td>
							<td><a href="#showDetail" data-toggle="modal" id="kegiatan" name="kegiatan" onclick="detaiKeg(<?php echo $keg['id_kegiatan']?>)"><?php echo $keg['nama_kegiatan']?></a></td>
							<td></td>
							<td><?php echo $keg['tot_apbdkab']?></td>
							<td><?php echo $keg['tot_apbdprov']?></td>
							<td><?php echo $keg['tot_apbn']?></td>
							<td><?php echo $keg['tot_apbn']?></td>
							<td style="text-align:center;">
                            <a href = "#collapseTwo" data-toggle="collapse" class="btn btn-small btn-info edit" name="<?php echo $keg['id_uskpd']; ?>"><i class="icon-edit icon-white"></i>&nbsp; Ubah</a>
                            <a class="btn btn-small btn-danger hapus" name="<?php echo $keg['id_uskpd'];?>" ><i class="icon-trash icon-white"></i>&nbsp; Hapus</a></td>
             
						</tr>
						<?
			}
		}
}
?>